// Copyright 2011, Vanya Davidenko.
// Кодировка файла: UTF-8. 
package info.iu9.red;



class NfaStatesSetList {
   public NfaStatesSetList(final int length) {
    dense_ = new NfaState[length];
    sparse_ = new int[length];
    clear();
  }

  boolean contains(final int value) {
    return value < capacity() &&
           sparse_[value] < size_ &&
           dense_[sparse_[value]].state_id() == value;
  }

  boolean push(final NfaState value) {
    if ( contains(value.state_id()) ) { return false; }
    pushNew(value);
    return true;
  }
  void pushNew(final NfaState value) {
    assert !contains(value.state_id());
    sparse_[value.state_id()] = size_;
    dense_[size_++] = value;
  }

  NfaState top() {
    assert !isEmpty();
    return dense_[size_ - 1];
  }
  NfaState pop() {
    assert !isEmpty();
    return dense_[--size_];
  }

   void clear() {
    size_ = 0;
  }

  boolean isEmpty() {
    return size_ == 0;
  }
  int size() {
    return size_;
  }
  int capacity() {
    return sparse_.length;
  }

  void swap(final NfaStatesSetList rhv) {
    final NfaState[] tmp_dense = rhv.dense_;
    rhv.dense_ = dense_;
    dense_ = tmp_dense;

    final int[] tmp_sparse = rhv.sparse_;
    rhv.sparse_ = sparse_;
    sparse_ = tmp_sparse;

    final int tmp_size = rhv.size_;
    rhv.size_ = size_;
    size_ = tmp_size;
  }


  private NfaState[] dense_;   // список
  private int[] sparse_;  // множество
  private int size_;
}
